/*
 * Created by JFormDesigner on Sun Dec 27 15:52:50 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.teacherInfo.changes;

import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.controller.TeacherController;
import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.entity.Teacher;
import cn.fourhundredandeighteen.utill.NumberUtil;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Br.S
 */
public class upStudentInfo extends JPanel {
    TeacherController teacherController=new TeacherController();
    private Long teacherId;
    private Teacher teacher;
    private JFrame jFrame;
    public upStudentInfo(Long teacherId,JFrame jFrame) {
        this.jFrame=jFrame;
        this.teacherId=teacherId;
        initComponents();
        if (teacherId!=0) {
            setUpText();
        }else{
            setSaveText();
        }
    }

    private void setUpText(){
        Result<Teacher> teacherResult = teacherController.teacherInfo(teacherId);
        if (teacherResult.getCode()!=2000){
            this.setVisible(false);
            JOptionPane.showConfirmDialog(null,"用户不存在！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }else {
            //设置文本内容
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
            Teacher data = teacherResult.getData();
            data.setUpdateTime(new Date());
            teacher=data;
            textField1.setText(String.valueOf(data.getTeacherId()));
            textField1.setEditable(false);
            textField2.setText(data.getTeacherName());
            textField3.setText(data.getTeacherAge());
            textField5.setText(String.valueOf(data.getTeacherSchool()));
            updataTime.setText(dateFormat.format(data.getUpdateTime()));
            updataTime.setEditable(false);
            studentClass.setText(data.getTeacherTitle());
            studentGender.setText(String.valueOf(data.getTeacherPassword()));
            studentMajor.setText(String.valueOf(data.getTeacherPhone()));
        }
    }

    private void setSaveText(){
            //设置文本内容
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
            Teacher teacher=new Teacher();
            teacher.setUpdateTime(new Date());
            teacher.setCreateTime(new Date());
            this.teacher=teacher;
            textField1.setEditable(false);
            updataTime.setText(dateFormat.format(new Date()));
            updataTime.setEditable(false);
    }

    private void button1ActionPerformed(ActionEvent e) {
        //功能判断
        //格式判断
        if (!NumberUtil.Password(studentGender.getText())){
            JOptionPane.showConfirmDialog(null, "密码格式错误！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }else if(NumberUtil.Age(textField3.getText())){
            JOptionPane.showConfirmDialog(null, "年龄错误！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }else{
            //赋值
            //姓名
            teacher.setTeacherName(textField2.getText());
            //年龄
            teacher.setTeacherAge(textField3.getText());
            //密码
            teacher.setTeacherPassword(studentGender.getText());
            //学校
            teacher.setTeacherSchool(textField5.getText());
            //手机号码
            teacher.setTeacherPhone(Integer.valueOf(studentMajor.getText()));
            //职称
            teacher.setTeacherTitle(studentClass.getText());
            //更新or上传
            Result result;
            if (teacherId != 0) {
                result = teacherController.upTeacher(teacher);
            }else{
                result = teacherController.CreateTeacherCourse(teacher);
            }
            if (result.getCode() == 2000) {
                JOptionPane.showConfirmDialog(null, "修改成功！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                Container.getInstance().upDatePage();
            } else {
                JOptionPane.showConfirmDialog(null, "修改失败！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
            }
            jFrame.setVisible(false);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        label4 = new JLabel();
        studentGender = new JTextField();
        label5 = new JLabel();
        textField5 = new JTextField();
        label6 = new JLabel();
        studentMajor = new JTextField();
        label7 = new JLabel();
        studentClass = new JTextField();
        label8 = new JLabel();
        updataTime = new JTextField();
        updata = new Button();
        label9 = new JLabel();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {87, 69, 0, 0, 207, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {43, 55, 55, 51, 55, 55, 54, 55, 53, 37, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText("\u5b66\u53f7");
        label1.setMaximumSize(new Dimension(23, 25));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField1, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label2 ----
        label2.setText("\u59d3\u540d");
        add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField2, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label3 ----
        label3.setText("\u5e74\u9f84");
        add(label3, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField3, new GridBagConstraints(2, 3, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label4 ----
        label4.setText("\u5bc6\u7801");
        add(label4, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(studentGender, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label5 ----
        label5.setText("\u5b66\u6821");
        add(label5, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField5, new GridBagConstraints(2, 5, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label6 ----
        label6.setText("\u624b\u673a\u53f7");
        add(label6, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(studentMajor, new GridBagConstraints(2, 6, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label7 ----
        label7.setText("\u804c\u79f0");
        add(label7, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(studentClass, new GridBagConstraints(2, 7, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label8 ----
        label8.setText("\u4e0a\u6b21\u4fee\u6539");
        add(label8, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(updataTime, new GridBagConstraints(2, 8, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- updata ----
        updata.setText("\u4fee\u6539");
        updata.addActionListener(e -> button1ActionPerformed(e));
        add(updata, new GridBagConstraints(2, 9, 2, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 15), 0, 0));
        add(label9, new GridBagConstraints(4, 9, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JLabel label4;
    private JTextField studentGender;
    private JLabel label5;
    private JTextField textField5;
    private JLabel label6;
    private JTextField studentMajor;
    private JLabel label7;
    private JTextField studentClass;
    private JLabel label8;
    private JTextField updataTime;
    private JButton updata;
    private JLabel label9;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
